<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" :width="1400">
    <el-form
      class="mb-10px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="供应商编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入供应商编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="供应商分类" prop="supplierTypeName">
        <el-input
          v-model="queryParams.supplierTypeName"
          placeholder="请输入供应商分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="业务员" prop="salesmanName">
        <el-input
          v-model="queryParams.salesmanName"
          placeholder="请输入业务员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="物质分类" prop="goodsType">
        <el-input
          v-model="queryParams.goodsType"
          placeholder="请输入物质分类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTableRef"
      v-loading="loading"
      :data="supplierData"
      border
      style="width: 100%"
      :max-height="400"
      @select="handleSelectionChange"
      class="table-style"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="供应商编号" align="center" prop="code" width="200" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="供应商分类" align="center" prop="supplierTypeName" />
      <el-table-column label="业务员" align="center" prop="salesmanName" width="150" />
      <el-table-column label="供应商状态" align="center" prop="status" width="120">
        <template #default="scope">
          <dict-tag type="dict_supplier_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="物质分类" align="center" prop="goodsType" width="100" />
    </el-table>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as OutboundTaskOrderApi from '@/api/outsource/outsourceIndentManage/outboundTaskOrder'

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('添加供应商') // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const supplierData = ref([]) // 机加数据
const queryParams = reactive({
  code: undefined,
  supplierName: undefined,
  supplierTypeName: undefined,
  salesmanName: undefined,
  status: 2,
  goodsType: undefined
})
const queryFormRef = ref() // 搜索的表单
const multipleTableRef = ref()

/** 打开弹窗 */
const open = async (val: number) => {
  dialogVisible.value = true
  // 修改时，设置数据
  loading.value = true
  try {
    await getList()
    nextTick(() => {
      supplierData.value.map((item) => {
        if (val === item['deptId']) {
          multipleTableRef.value!.toggleRowSelection(item, true)
        }
      })
    })
  } finally {
    loading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 获取数据
const getList = async () => {
  loading.value = true
  try {
    const data = await OutboundTaskOrderApi.getOutboundTaskOrderSrmList(queryParams)
    supplierData.value = data
  } finally {
    loading.value = false
  }
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  loading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', checkedGh.value)
  } finally {
    loading.value = false
  }
}

/**单选 */
const checkedGh = ref()
const handleSelectionChange = (selection, row) => {
  multipleTableRef.value.clearSelection()
  if (selection.length === 0) {
    checkedGh.value = []
    return
  }
  checkedGh.value = row
  multipleTableRef.value.toggleRowSelection(row, true)
}
/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
<style lang="scss" scoped>
.table-style ::v-deep .el-table-column--selection.is-leaf .el-checkbox {
  display: none;
}
</style>
